home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_02 / 1102069a < prev    next >
Text File  |  1992-12-12  |  14KB  |  467 lines

  1.     /**********************************************
  2.     *
  3.     *  grow(...
  4.     *
  5.     *  This function is an object detector.
  6.     *  Its input is an binary image array 
  7.     *  containing 0's and value's.
  8.     *  It searches through the image and connects
  9.     *  the adjacent values.
  10.     *
  11.     ***********************************************/
  12.  
  13. grow(binary, value)
  14.    short binary[ROWS][COLS],
  15.          value;
  16. {
  17.    char name[80];
  18.  
  19.    int first_call,
  20.        i,
  21.        j,
  22.        object_found,
  23.        pointer,
  24.        pop_i,
  25.        pop_j,
  26.        stack_empty,
  27.        stack_file_in_use;
  28.  
  29.    short g_label, stack[STACK_SIZE][2];
  30.  
  31.             /*************************************
  32.             *
  33.             *   Now begin the process of growing
  34.             *   regions.
  35.             *
  36.             **************************************/
  37.  
  38.    g_label       = 2;
  39.    object_found  = 0;
  40.    first_call    = 1;
  41.  
  42.    for(i=0; i<ROWS; i++){
  43.       for(j=0; j<COLS; j++){
  44.          stack_file_in_use =  0;
  45.          stack_empty       =  1;
  46.          pointer           = -1;
  47.  
  48.                /**********************************
  49.                *
  50.                *  Search for the first pixel of
  51.                *  a region.
  52.                *
  53.                ***********************************/
  54.  
  55.          if(binary[i][j] == value){
  56.             label_and_check_neighbor(binary, stack, g_label,
  57.                            &stack_empty, &pointer, i, j,
  58.                            value, &stack_file_in_use,
  59.                            &first_call);
  60.             object_found = 1;
  61.          }  /* ends if binary[i]j] == value */
  62.  
  63.                /*****************************
  64.                *
  65.                *  If the stack is not empty,
  66.                *  pop the coordinates of
  67.                *  the pixel off the stack
  68.                *  and check its 8 neighbors.
  69.                *
  70.                *******************************/
  71.  
  72.          while(stack_empty == 0){
  73.             pop_i = stack[pointer][0]; /* POP       */
  74.             pop_j = stack[pointer][1]; /* OPERATION */
  75.             --pointer;
  76.             if(pointer <= 0){
  77.                if(stack_file_in_use){
  78.                   pop_data_off_of_stack_file(
  79.                                  stack,
  80.                                  &pointer,
  81.                                  &stack_file_in_use);
  82.                }  /* ends if stack_file_in_use  */
  83.                else{
  84.                   pointer     = 0;
  85.                   stack_empty = 1;
  86.                }  /* ends else stack file is
  87.                      not in use  */
  88.             }  /*  ends if point <= 0  */
  89.  
  90.             label_and_check_neighbor(binary,
  91.                         stack, g_label,
  92.                         &stack_empty,
  93.                         &pointer, pop_i,
  94.                         pop_j, value,
  95.                         &stack_file_in_use,
  96.                         &first_call);
  97.          }  /* ends while stack_empty == 0 */
  98.  
  99.          if(object_found == 1){
  100.             object_found = 0;
  101.             ++g_label;
  102.          }  /* ends if object_found == 1 */
  103.  
  104.       }   /* ends loop over j */
  105.    }  /* ends loop over i */
  106.  
  107.    printf("\nGROW> found %d objects", g_label);
  108.  
  109. } /* ends grow  */
  110.  
  111.    /********************************************
  112.    *
  113.    *  label_and_check_neighbors(...
  114.    *
  115.    *  This function labels a pixel with an object
  116.    *  label and then checks the pixel's 8
  117.    *  neighbors.  If any of the neigbors are
  118.    *  set, then they are also labeled.
  119.    *
  120.    ***********************************************/
  121.  
  122. label_and_check_neighbor(binary_image, stack,
  123.                          g_label, stack_empty,
  124.                          pointer, r, e, value,
  125.                          stack_file_in_use,
  126.                          first_call)
  127. int   e,
  128.       *first_call,
  129.       *pointer,
  130.       r,
  131.       *stack_empty,
  132.       *stack_file_in_use;
  133.  
  134. short binary_image[ROWS][COLS],
  135.       g_label,
  136.       stack[STACK_SIZE][2],
  137.       value;
  138. {
  139.    int already_labeled = 0,
  140.        i, j;
  141.  
  142.    if (binary_image[r][e] == g_label)
  143.       already_labeled = 1;
  144.  
  145.    binary_image[r][e] = g_label;
  146.  
  147.       /***************************************
  148.       *
  149.       *   Look at the 8 neighors of the
  150.       *   point r,e.
  151.       *
  152.       *   Ensure the points you are checking
  153.       *   are in the image, i.e. not less
  154.       *   than zero and not greater than
  155.       *   ROWS-1 or COLS-1.
  156.       *
  157.       ***************************************/
  158.  
  159.    for(i=(r-1); i<=(r+1); i++){
  160.       for(j=(e-1); j<=(e+1); j++){
  161.  
  162.          if((i>=0)   &&
  163.             (i<=ROWS-1)  &&
  164.             (j>=0)   &&
  165.             (j<=COLS-1)){
  166.  
  167.             if(binary_image[i][j] == value){
  168.                *pointer           = *pointer + 1;
  169.                stack[*pointer][0] = i; /* PUSH      */
  170.                stack[*pointer][1] = j; /* OPERATION */
  171.                *stack_empty       = 0;
  172.  
  173.                if(*pointer >= (STACK_SIZE -
  174.                                STACK_FILE_LENGTH)){
  175.                   push_data_onto_stack_file(stack,
  176.                             pointer, first_call);
  177.                   *stack_file_in_use = 1;
  178.                }  /* ends if *pointer >=
  179.                      STACK_SIZE - STACK_FILE_LENGTH*/
  180.  
  181.             }  /* end of if binary_image == value */
  182.          }  /* end if i and j are on the image */
  183.       }  /* ends loop over i rows           */
  184.    }  /* ends loop over j columns        */
  185. }  /* ends label_and_check_neighbors  */
  186.  
  187.    /****************************************
  188.    *
  189.    *   push_data_onto_stack_file(...
  190.    *
  191.    *   This function takes the stack array
  192.    *   and pushes it onto the stack file.
  193.    *
  194.    *****************************************/
  195.  
  196. push_data_onto_stack_file(stack, pointer, first_call)
  197.    int   *first_call, *pointer;
  198.    short stack[STACK_SIZE][2];
  199. {
  200.    char  backup_file_name[MAX_NAME_LENGTH];
  201.    FILE  *backup_file_pointer, *stack_file_pointer;
  202.    int   diff, i;
  203.    short holder[STACK_FILE_LENGTH][2];
  204.  
  205.    printf("\nSFO> Start of push_data_onto_stack ");
  206.  
  207.    diff = STACK_SIZE - STACK_FILE_LENGTH;
  208.  
  209.        /*******************************************
  210.        *
  211.        *   Copy the elements to be stored to the
  212.        *   stack file into holder
  213.        *
  214.        ********************************************/
  215.  
  216.    for(i=0; i<STACK_FILE_LENGTH; i++){
  217.       holder[i][0] = stack[i][0];
  218.       holder[i][1] = stack[i][1];
  219.    }
  220.        /*******************************************
  221.        *
  222.        *   Move the elements of the stack down
  223.        *
  224.        *******************************************/
  225.  
  226.    for(i=0; i<diff; i++){
  227.       stack[i][0] = stack[i + STACK_FILE_LENGTH][0];
  228.       stack[i][1] = stack[i + STACK_FILE_LENGTH][1];
  229.    }
  230.  
  231.        /*******************************************
  232.        *
  233.        *   Fill the top of the stack with zeros
  234.        *
  235.        *******************************************/
  236.  
  237.    for(i=diff; i<STACK_SIZE; i++){
  238.       stack[i][0] = 0;
  239.       stack[i][1] = 0;
  240.    }
  241.  
  242.    *pointer = *pointer - STACK_FILE_LENGTH;
  243.  
  244.        /************************************************
  245.        *
  246.        *   Store the holder array into the stack file.
  247.        *   Open the stack file for writing in binary
  248.        *   mode. If the file does not exist it will be
  249.        *   created. If the file does exist it will be
  250.        *   over written.
  251.        *
  252.        *   PUSH - IF first_time == 1 then write to stack
  253.        *          ELSE write to stack.bak
  254.        *          append stack onto stack.bak
  255.        *          copy stack.bak to stack
  256.        *          this has the effect of writing
  257.        *          to the beginning of the stack.
  258.        *
  259.        ************************************************/
  260.  
  261.    if(*first_call == 1){
  262.  
  263.       *first_call = *first_call + 1;
  264.       if((stack_file_pointer = fopen(STACK_FILE,"wb"))
  265.                                      == NULL)
  266.          printf("\nSFO> Could not open stack file");
  267.       else{
  268.          /*printf("\n\nSFO> Writing to stack file");*/
  269.          fwrite(holder, sizeof(holder),
  270.                 1, stack_file_p